<template>
  <div class="Data_Source_Management">
    <div class="pubBig Privilege">
      <div class="h0">
        <p class="lt"></p>
      </div>
      <div class="dDownMain">
        <div
          class="twoBox"
          :style="
            this.$route.query.Token
              ? 'top:50px;left:10px'
              : 'top: 100px;padding:10px'
          "
        >
          <el-row>
            <el-col :span="12" v-if="false">
              <el-button size="small">全部</el-button>
              <el-button size="small">待验证</el-button>
              <el-button size="small">未通过</el-button>
              <el-button size="small">已通过</el-button>
            </el-col>
            <el-col :span="24">
              <el-input
                size="small"
                style="display: inline-block; width: 20%; margin-right: 10px"
                placeholder="请输入应用名称查询"
                v-model="search_val"
              ></el-input>
              <el-button
                size="small"
                icon="el-icon-search"
                @click="(page.page = 1), getTableData()"
              ></el-button>
              <el-button
                style="float: right; margin-right: 5px"
                size="small"
                icon="el-icon-plus"
                @click="open"
                >新增数据源</el-button
              >
            </el-col>
          </el-row>
          <template>
            <el-table
              :data="tableData"
              style="width: 100%; margin-top: 10px"
              v-loading="isLoading"
              height="600"
            >
              <el-table-column prop="ApplicationName" label="应用名称">
              </el-table-column>
              <el-table-column prop="Type" label="映射类型">
                <template slot-scope="scope">
                  {{
                    scope.row.Type == 1
                      ? "用户"
                      : scope.row.Type == 2
                      ? "值域"
                      : scope.row.Type == 3
                      ? "数据集"
                      : "未知"
                  }}
                </template>
              </el-table-column>
              <el-table-column prop="DbType" label="数据库类型">
              </el-table-column>
              <el-table-column prop="DbIp" label="IP地址"> </el-table-column>
              <el-table-column prop="DbName" label="数据源名称">
              </el-table-column>
              <el-table-column prop="Name" label="数据库名/服务名">
              </el-table-column>
              <el-table-column prop="DbPassword" label="密码">
                <template slot-scope="">
                  <span>******</span>
                </template>
              </el-table-column>
              <el-table-column prop="DbPort" label="端口号"> </el-table-column>
              <el-table-column label="验证状态">
                <template slot-scope="scope">
                  <span style="color: #006600">{{
                    scope.row.Status == 1 ? "成功" : "失败"
                  }}</span>
                </template>
              </el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    icon="el-icon-edit"
                    @click="edit(scope.row)"
                  ></el-button>
                  <el-button
                    size="mini"
                    icon="el-icon-delete"
                    @click="del(scope.row.Id)"
                  ></el-button>
                </template>
              </el-table-column>
            </el-table>
          </template>
          <!-- 页码 -->
          <div class="block" style="text-align: center">
            <el-pagination
              layout="prev, pager, next"
              :total="page.total"
              :page-size="page.limit"
              :current-page="page.page"
              @current-change="page_change"
            >
            </el-pagination>
          </div>
        </div>
        <!-- 弹层 -->
        <el-dialog
          :title="title"
          :visible.sync="dialogVisible"
          width="45%"
          :before-close="handleClose"
          :append-to-body="true"
          :close-on-click-modal="false"
        >
          <div v-loading="AllLoading">
            <el-form
              label-position="right"
              label-width="130px"
              :model="formLabelAlign"
            >
              <el-form-item
                label="*应用"
                style="display: inline-block; width: 50%"
              >
                <el-select
                  filterable
                  v-model="formLabelAlign.ApplicationId"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in Application_List"
                    :key="item.Id"
                    :label="item.Name"
                    :value="item.Id + ''"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item
                label="*映射类型"
                style="display: inline-block; width: 50%"
              >
                <el-select
                  filterable
                  v-model="formLabelAlign.Type"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option label="用户" :value="1"> </el-option>
                  <el-option label="值域" :value="2"> </el-option>
                  <el-option label="数据集" :value="3"> </el-option>
                </el-select>
              </el-form-item>
              <el-form-item
                label="*数据库别名"
                style="display: inline-block; width: 50%"
              >
                <el-input v-model="formLabelAlign.Name"></el-input>
              </el-form-item>
              <el-form-item
                label="*数据库名称"
                style="display: inline-block; width: 50%"
              >
                <el-input v-model="formLabelAlign.DbName"></el-input>
              </el-form-item>
              <el-form-item
                label="*数据库类型"
                style="display: inline-block; width: 50%"
              >
                <el-select
                  filterable
                  v-model="formLabelAlign.DbType"
                  placeholder="请选择"
                  @change="formLabelAlign_DbType_change"
                  style="width: 100%"
                >
                  <el-option label="SqlServer" value="SqlServer"> </el-option>
                  <el-option label="MySQL" value="MySQL"> </el-option>
                  <el-option label="Oracle" value="Oracle"> </el-option>
                </el-select>
              </el-form-item>
              <el-form-item
                label="*IP地址"
                style="display: inline-block; width: 50%"
              >
                <el-input v-model="formLabelAlign.DbIp"></el-input>
              </el-form-item>
              <el-form-item
                label="*数据库用户名"
                style="display: inline-block; width: 50%"
              >
                <el-input v-model="formLabelAlign.DbUser"></el-input>
              </el-form-item>

              <el-form-item
                label="*数据库密码"
                style="display: inline-block; width: 50%"
              >
                <el-input
                  v-model="formLabelAlign.DbPassword"
                  type="password"
                ></el-input>
              </el-form-item>
              <el-form-item
                label="*数据库端口号"
                style="display: inline-block; width: 50%"
              >
                <el-input
                  v-model="formLabelAlign.DbPort"
                  @input="DbPort"
                ></el-input>
              </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
              <el-button @click="handleClose">取 消</el-button>
              <el-button type="primary" @click="btn_Ok">确 定</el-button>
            </span>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "",
  data() {
    return {
      tableData: [],
      search_val: "",
      page: {
        page: 1,
        limit: 10,
        total: 1,
      },
      dialogVisible: false,
      formLabelAlign: {
        DbPort: "",
      },
      Application_List: [],
      isLoading: false,
      title: "",
      AllLoading: false,
    };
  },
  methods: {
    getTableData() {
      // 获取表格数据
      this.isLoading = true;
      this.$post(
        "/ServiceManage/Db/List?keyword=" +
          this.search_val +
          "&page=" +
          this.page.page +
          "&limit=" +
          this.page.limit
      ).then((res) => {
        if (res.code == 0) {
          this.tableData = res.data.list;
          this.isLoading = false;
        }
      });
    },
    page_change(val) {
      // 页码切换
      this.page.page = val;
      this.getTableData();
    },
    open() {
      // 打开弹窗
      this.formLabelAlign.Id ? (this.title = "修改") : (this.title = "新增");
      this.dialogVisible = true;
    },
    handleClose() {
      // 关闭弹窗
      this.dialogVisible = false;
      this.formLabelAlign = {
        DbPort: "",
      };
    },
    btn_Ok() {
      // 弹窗确定按钮
      if (
        this.formLabelAlign.ApplicationId == null ||
        this.formLabelAlign.ApplicationId == "" ||
        this.formLabelAlign.Type == null ||
        this.formLabelAlign.Type == "" ||
        this.formLabelAlign.Name == null ||
        this.formLabelAlign.Name == "" ||
        this.formLabelAlign.DbName == null ||
        this.formLabelAlign.DbName == "" ||
        this.formLabelAlign.DbIp == null ||
        this.formLabelAlign.DbIp == "" ||
        this.formLabelAlign.DbUser == null ||
        this.formLabelAlign.DbUser == "" ||
        this.formLabelAlign.DbPassword == null ||
        this.formLabelAlign.DbPassword == "" ||
        this.formLabelAlign.DbPort == null ||
        this.formLabelAlign.DbPort == ""
      ) {
        this.$message({
          message: "请完整填写必填项！",
          type: "error",
        });
        return;
      }
      this.AllLoading = true;
      let url = this.formLabelAlign.Id
        ? "/ServiceManage/Db/Update"
        : "/ServiceManage/Db/Create";
      this.$post(url, this.formLabelAlign).then((res) => {
        if (res.code == 0) {
          this.handleClose();
          this.getTableData();
          this.$message({
            message: "操作成功",
            type: "success",
          });
          this.AllLoading = false;
        } else {
          this.AllLoading = false;
          this.$message({
            message: res.msg,
            type: "error",
          });
        }
      });
    },
    getApplication_List() {
      // 获取应用数据
      this.$post("/ServiceManage/Application/List?page=1&limit=999").then(
        (res) => {
          if (res.code == 0) {
            this.Application_List = res.data.list;
          }
        }
      );
    },
    del(id) {
      // 表格删除操作事件
      this.$confirm("确定删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then((_) => {
          this.$post("/ServiceManage/Db/Delete/" + id).then((res) => {
            if (res.code == 0) {
              this.getTableData();
              this.$message({
                message: "删除成功",
                type: "success",
              });
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        })
        .catch((_) => {});
    },
    edit(val) {
      // 表格修改操作事件
      var info = JSON.stringify(val);
      this.formLabelAlign = JSON.parse(info);
      this.open();
    },
    formLabelAlign_DbType_change(val) {
      // 数据类型改变自动填充端口号方法
      if (val == "SqlServer") {
        this.formLabelAlign.DbPort = "1433";
      }
      if (val == "MySQL") {
        this.formLabelAlign.DbPort = "3306";
      }
      if (val == "Oracle") {
        this.formLabelAlign.DbPort = "1521";
      }
    },
    DbPort(val) {
      console.log(val);
    },
  },
  mounted() {
    // document.getElementById("2").focus();
  },
  created() {
    if (this.$route.query.Token) {
      localStorage.setItem("Token", this.$route.query.Token);
    }
    this.getTableData();
    this.getApplication_List();
  },
};
</script>
<style lang="scss">
.Data_Source_Management {
  .top-input {
    display: inline-block;
    width: 350px;
  }

  .box-top {
    margin-top: 15px;
    margin-left: 5px;
  }
  .el-select-dropdown .el-scrollbar .el-scrollbar__wrap {
    overflow: scroll !important;
  }
}
</style>
